#include "dialog_set.h"
#include "ui_dialog_set.h"
#include "mysql.h"
#include "dialog_msbox.h"
Dialog_set::Dialog_set(test *da,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_set)
{
    ui->setupUi(this);

    buffer=da;

    this->setWindowFlags(Qt::FramelessWindowHint);
    t_btn_no=ui->btn_no;
    connect(ui->btn_no,&QPushButton::clicked,[=]{done(ENM_no_BTN);});

    ui->system_user->setText(buffer->system.system_user);
    ui->system_jrs->setText(QString("%1").arg(buffer->system.system_jrs));
    ui->system_bjs->setText(QString("%1").arg(buffer->system.system_bjs));
    ui->system_tjw1->setText(QString("%1").arg(buffer->system.system_tjw1));
    ui->system_tjw2->setText(QString("%1").arg(buffer->system.system_tjw2));
    ui->system_rrl1->setText(QString("%1").arg(buffer->system.system_rrl1));
    ui->system_rrl2->setText(QString("%1").arg(buffer->system.system_rrl2));
    ui->system_zsti->setText(QString("%1").arg(buffer->system.system_zsti));
    ui->system_psti->setText(QString("%1").arg(buffer->system.system_psti));

    QSettings *set_ini=new QSettings(QCoreApplication::applicationDirPath()+"/ini_file.ini",QSettings::IniFormat);
    set_ini->setIniCodec(QTextCodec::codecForName("GB2312"));
    buffer->temp_ctrl=set_ini->value("temp_ctrl").toDouble();
    buffer->tec_control=set_ini->value("temp_check").toBool();

    delete set_ini;


    ui->set_temp_water->setText(QString("%1").arg(buffer->temp_ctrl));
    ui->check_temp->setChecked(buffer->tec_control);

    ui->system_user->setEnabled(false);
    ui->system_jrs->setEnabled(false);
    ui->system_bjs->setEnabled(false);
    ui->system_tjw1->setEnabled(false);
    ui->system_tjw2->setEnabled(false);
    ui->system_rrl1->setEnabled(false);
    ui->system_rrl2->setEnabled(false);
    ui->system_zsti->setEnabled(false);
    ui->system_psti->setEnabled(false);
    ui->system_repass->setEnabled(false);
    ui->reset->setEnabled(false);
    ui->system_set->setEnabled(false);
    ui->save->setEnabled(false);
}

Dialog_set::~Dialog_set()
{
    delete ui;
}


int Dialog_set::show(test *buffer, QWidget *parent)
{
    Dialog_set box(buffer);
    return box.exec();
}

void Dialog_set::on_save_clicked()
{

    int ret=Dialog_msbox::show(1,1,"系统设置","System settings","请注意，是否对更改的选项进行保存？");
    if(ret==1)
    {
        buffer->system.system_user=ui->system_user->text();
        buffer->system.system_jrs=ui->system_jrs->text().toDouble();
        buffer->system.system_bjs=ui->system_bjs->text().toDouble();
        buffer->system.system_tjw1=ui->system_tjw1->text().toDouble();
        buffer->system.system_tjw2=ui->system_tjw2->text().toDouble();
        buffer->system.system_rrl1=ui->system_rrl1->text().toDouble();
        buffer->system.system_rrl2=ui->system_rrl2->text().toDouble();
        buffer->system.system_zsti=ui->system_zsti->text().toInt();
        buffer->system.system_psti=ui->system_psti->text().toInt();

        mysql::INSERT_system(buffer->system);

        buffer->temp_ctrl=ui->set_temp_water->text().toDouble();
        buffer->tec_control=ui->check_temp->isChecked();

        QSettings *set_ini=new QSettings(QCoreApplication::applicationDirPath()+"/ini_file.ini",QSettings::IniFormat);
        set_ini->setIniCodec(QTextCodec::codecForName("GB2312"));
        set_ini->setValue("temp_ctrl",buffer->temp_ctrl);
        set_ini->setValue("temp_check",buffer->tec_control);
        delete set_ini;


        Dialog_msbox::show(0,0,"信息保存","Information saving","系统参数保存完成。");
    }


}

void Dialog_set::on_login_clicked()
{
    if(buffer->system.system_pass==ui->system_pass->text())
    {
        ui->system_user->setEnabled(true);
        ui->system_jrs->setEnabled(true);
        ui->system_bjs->setEnabled(true);
        ui->system_tjw1->setEnabled(true);
        ui->system_tjw2->setEnabled(true);
        ui->system_rrl1->setEnabled(true);
        ui->system_rrl2->setEnabled(true);
        ui->system_zsti->setEnabled(true);
        ui->system_psti->setEnabled(true);
        ui->system_repass->setEnabled(true);
        ui->reset->setEnabled(true);
        ui->system_set->setEnabled(true);
        ui->save->setEnabled(true);

    }

}

void Dialog_set::on_system_set_clicked()
{

    int ret=Dialog_msbox::show(1,1,"系统","数据初始化",QString("请注意，是否对系统进行初始化？继续将删除所有数据？"));
    if(ret==1)
    {
        if(mysql::SYSTEM_LOAD())
        {
            test_system data("admin123456","四川法瑞检测设备有限公司",150,26543,100,100,
                             9542,0,35,50,"COM1","127.0.0.1","502");

            mysql::INSERT_system(data);

            Dialog_msbox::show(0,0,"系统","数据初始化","系统数据初始化成功！");
        }
    }
}

void Dialog_set::on_reset_clicked()
{
    buffer->system.system_pass=ui->system_repass->text();
     mysql::INSERT_system(buffer->system);
}
